import { getToken } from '@/utils/auth'

// 登录页面
const loginPage = '/pages/login'

// 页面白名单

const whiteList = ['/pages/login', '/pages/change-pwd', '/pages/common/webview/index', '/pages/upgrade/upgrade']

// 检查地址白名单

function checkWhite(url) {
  const path = url.split('?')[0]
  return whiteList.indexOf(path) !== -1
}

// 页面跳转验证拦截器

const list = ['navigateTo', 'redirectTo', 'switchTab']
list.forEach((item) => {
  uni.addInterceptor(item, {
    invoke(e) {
      const url = e.url.split('?')[0]
      const notNeed = whiteList.includes(url)
      if (notNeed) {
        if (getToken()) {
          uni.switchTab({
            url: '/pages/messages/index'
          })
          return false
        }
        return e
      } else {
        if (!getToken()) {
          uni.reLaunch({
            url: loginPage
          })
          return false
        } else {
          return e
        }
      }
    }
  })
})
